package com.cg.big_event.service.impl;

import com.cg.big_event.entity.User;
import com.cg.big_event.mapper.UserMapper;
import com.cg.big_event.service.UserService;
import com.cg.big_event.utils.Md5Util;
import com.cg.big_event.utils.ThreadLocalUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Service
public class UserServiceImpl implements UserService {
    private static final Logger logger = LoggerFactory.getLogger ( UserServiceImpl.class );
    @Resource
    private UserMapper userMapper;

    @Override
    public User findUserByName(String username) {
        User user = userMapper.findUserByName ( username );
        return user;
    }

    @Override
    public void register(String username, String password) {
        //加密
        String md5PwdStr = Md5Util.getMD5String ( password );
        userMapper.add ( username, md5PwdStr );
    }

    @Override
    public void update(User user) {
        user.setUpdateTime ( LocalDate.from ( LocalDateTime.now () ) );
        userMapper.update ( user );
    }

    @Override
    public void updateAvater(String avatarUrl) {
        //因为我们从ThreadLocal中获取用户的用户id值是String类型的而不是int类型的，所以需要转换一下，但是我们使用(Integer)
        // 直接进行强制转换会报错，所以需要使用instanceof判断一下，如果idObj是Integer类型的就直接强转，如果是String类型的就使用parseInt方法进行转换。
        Map<String, Object> map = ThreadLocalUtil.get ();
        Object idObj = map.get ( "id" );
        Integer id;
        if (idObj instanceof Integer) {
            id = (Integer) idObj;
        } else if (idObj instanceof String) {
            try {
                id = Integer.parseInt ( (String) idObj );
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException ( "Invalid integer value: " + idObj, e );
            }
        } else {
            throw new IllegalArgumentException ( "Unexpected type: " + idObj.getClass ().getName () );
        }
        userMapper.updateAvater ( avatarUrl, id );
    }

    @Override
    public void updatePwd(String newPwd) {
        //因为我们从ThreadLocal中获取用户的用户id值是String类型的而不是int类型的，所以需要转换一下，但是我们使用(Integer)
        // 直接进行强制转换会报错，所以需要使用instanceof判断一下，如果idObj是Integer类型的就直接强转，如果是String类型的就使用parseInt方法进行转换。
        Map<String, Object> map = ThreadLocalUtil.get ();
        Object idObj = map.get ( "id" );
        Integer id;
        if (idObj instanceof Integer) {
            id = (Integer) idObj;
        } else if (idObj instanceof String) {
            try {
                id = Integer.parseInt ( (String) idObj );
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException ( "Invalid integer value: " + idObj, e );
            }
        } else {
            throw new IllegalArgumentException ( "Unexpected type: " + idObj.getClass ().getName () );
        }
        String md5PwdStr = Md5Util.getMD5String (newPwd);
        userMapper.updatePwd ( md5PwdStr, id );
    }
}
